package main

import (
	"flag"
	"fmt"
	"os"
	"path"
	"strings"
)

const command = "basename"

var dir = flag.Bool("d", false, "print directory component")

func usage() {
	fmt.Fprint(os.Stderr, "usage: ", command, " [ -d ] string [ suffix ]\n")
	os.Exit(2)
}

func main() {
	flag.Usage = usage
	flag.Parse()
	if flag.NArg() < 1 || flag.NArg() > 2 {
		usage()
	}
	if *dir {
		fmt.Println(path.Dir(flag.Arg(0)))
		os.Exit(0)
	}
	result := path.Base(flag.Arg(0))
	if flag.NArg() == 2 {
		result = strings.TrimSuffix(result, flag.Arg(1))
	}
	fmt.Println(result)
	os.Exit(0)
}
